﻿namespace Blaze.Editor.CLI
{
    using System;
    using System.Diagnostics;

    /// <summary>
    /// 提供git命令行接口调用。
    /// </summary>
    public static class Git
    {
        /// <summary>
        /// git-rev-list - Lists commit objects in reverse chronological order.
        /// git rev-list HEAD --count
        /// </summary>
        /// <returns></returns>
        public static int RevListCount(string name = "HEAD")
        {
            var process = new Process
            {
                StartInfo =
                {
                    Arguments = string.Format("rev-list {0} --count", name),
                    FileName = mBinPath,
                    UseShellExecute = false,
                    CreateNoWindow = true,
                    RedirectStandardOutput = true,
                    RedirectStandardError = false,
                }
            };
            if (!process.Start())
                throw new ApplicationException("git rev-list failed -> " + name);
            var result = process.StandardOutput.ReadToEnd();
            process.WaitForExit();
            var exitCode = process.ExitCode;
            if (exitCode != 0)
                throw new ApplicationException("git rev-list failed -> exit code=" + exitCode);
            return int.Parse(result);
        }

        private static string mBinPath = "git.exe";
    }
}